package com.aifast.common.forUpdate;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.aifast.common.base.Kv;
import com.aifast.common.cache.util.RedisUtil;
import com.aifast.common.global.Const;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

/**
 * ForUpdate的自动初始化数据启动类，会在springboot启动时自动装载数据在redis缓存中
 */
@Slf4j
@Component
public class ForUpdateInitDataRunner implements ApplicationListener<ContextRefreshedEvent> {
    @Autowired
    private IForUpdateService service;
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        log.info("****开始初始化ForUpdate数据********************************************");
        Kv<String, String> tableNames = service.getAllTableNames();
        if(tableNames.size() == 0)return;
        tableNames.forEach((k,v)->{
            String tablename = StrUtil.toUnderlineCase(k);
            Kv<String, String> tableColumnComment = service.getTableColumnComment(tablename);
            String join = StrUtil.join(Const.SYMBOL_COLON, "AiFast", "forUpdate", tablename);
            RedisUtil.set(join, JSONUtil.toJsonStr(tableColumnComment));
        });
    }
}
